package cn.com.inossem.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import java.io.File;
import java.io.FileOutputStream;

import cn.com.inossem.systembartint.SystemBarTintManager;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

public class InviteShareDialogActivity extends Activity {
	TextView back;
	String shareImage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			setTranslucentStatus(true);
		}

		SystemBarTintManager tintManager = new SystemBarTintManager(this);
		tintManager.setStatusBarTintEnabled(true);
		tintManager.setTintColor(getResources().getColor(R.color.app_main));
		initImage();
		
		showShare();
		finish();

		/*setContentView(R.layout.shareactivity_dialog);
		ImageView shareweixin = (ImageView) this.findViewById(R.id.shareweixin);
		shareweixin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// finish();
				showShare();
			}
		});

		back = (TextView) this.findViewById(R.id.back);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});*/
	}
	
	private void initImage() {  
        try {// 判断SD卡中是否存在此文件夹  
            if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())  
                    && Environment.getExternalStorageDirectory().exists()) {  
                File baseFile = new File(Environment.getExternalStorageDirectory(), "share");  
                if (!baseFile.exists()) {  
                    baseFile.mkdir();  
                }  
                shareImage = baseFile.getAbsolutePath() + "/sapshare.png";  
            } else {  
            	shareImage = getApplication().getFilesDir().getAbsolutePath() + "/sapshare.png";  
            }  
            File file = new File(shareImage);
            if (file.exists()) {
            	file.delete();
            }
            // 判断图片是否存此文件夹中  
            if (!file.exists()) {  
                file.createNewFile();  
                Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.zhi);
                FileOutputStream fos = new FileOutputStream(file);  
                pic.compress(CompressFormat.PNG, 100, fos);  
                fos.flush();  
                fos.close();  
            }  
        } catch (Throwable t) {  
            t.printStackTrace();  
            shareImage = null;  
        }  
    }

	private void showShare() {
		ShareSDK.initSDK(this);
		OnekeyShare oks = new OnekeyShare();
		oks.setSilent(true);
//		oks.setPlatform(Wechat.NAME);
		// 关闭sso授权
		oks.disableSSOWhenAuthorize();
		// title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
		oks.setTitle("智仁：一个服务于咨询顾问的圈子，邀请您的加入！");
		// titleUrl是标题的网络链接，仅在人人网和QQ空间使用
		oks.setTitleUrl("http://www.inossem.com/ZProject/validWeXin.html");
		oks.setImagePath(shareImage);
		// url仅在微信（包括好友和朋友圈）中使用
		oks.setUrl("http://www.inossem.com/ZProject/validWeXin.html");
		// site是分享此内容的网站名称，仅在QQ空间使用
		oks.setSite(getString(R.string.app_name));
		// 启动分享GUI
		oks.show(this);
	}

	private void setTranslucentStatus(boolean on) {
		Window win = getWindow();
		WindowManager.LayoutParams winParams = win.getAttributes();
		final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
		if (on) {
			winParams.flags |= bits;
		} else {
			winParams.flags &= ~bits;
		}
		win.setAttributes(winParams);
	}
}
